layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '/MappingRules/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'rule_uid',  title: '规则ID', sort: true},
            {field: 'rule_name',  title: '规则名称', sort: true},
            {field: 'dealer',  title: '经销商', sort: true},
            // {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            var index = layer.open({
                title: '添加规则',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/MapRules-add.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.confirm('真的删除么?', function (index) {
                var checkStatus = table.checkStatus('currentTableId')
                            , data = checkStatus.data;
                // console.log("Debug data:",data);
                var tempname ;
                for (x = 0;x<data.length;x++){
                    if (typeof(tempname) == "undefined"){
                        // console.log("debug tempname:",tempname);
                        tempname = data[x].excel_name;
                    }else{
                        tempname = tempname + "," +data[x].rule_name;
                    }
                    
                }
                $.ajax({
                    url:'/MappingRules/del',
                    type:"post",
                    data:JSON.stringify(data),
                    dataType:"json",
                    contentType:'application/json',  // 声明发送数据格式
                    // processData:false,   // 浏览器不要影响数据
                });
                table.reload('currentTableId');
                layer.close(index);
            });


        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            console.log("Debug edit obj:",obj);
            var index = layer.open({
                title: '编辑规则',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/MapRules-edit.html?id=' + obj.data.rule_uid,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除么?', function (index) {
                obj.del();
                layer.close(index);

                // console.log("Debug obj:",obj.data.rule_uid);
                $.ajax({
                url:'/MappingRules/del',
                type:"post",
                data:JSON.stringify([obj.data]),
                dataType:"json",
                contentType:'application/json',  // 声明发送数据格式
                });
            });
        }
    });

});